home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- Copyright (c) 2008 Ensolis, LLC. All Rights Reserved.
- ----------------------------------------------------------------------------*/
-
- var gLabels = null;
-
- function labelsLoad()
- {
- gLabels = new LabelsModule();
- gLabels.start();
- }
-
- function labelsUnload()
- {
- gLabels.stop();
- gLabels = null;
- }
-
- function LabelsModule() {}
- LabelsModule.prototype = {
- _manager: null,
- _clipboard: null,
- _bundle: null,
- _types: null,
- _defaults: null,
- _rows: null,
- _sortId: null,
- _sortDirection: null,
- _naturalOrder: null,
- _columns: new Array("ff-col-variable", "ff-col-description", "ff-col-value"),
-
- start: function LabelsModule_start()
- {
- //setup variables
- this._manager = Components.classes["@ensolis.com/forecastfox/manager-service;1"].getService(Components.interfaces.ffIManagerService);
- this._clipboard = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
- this._bundle = document.getElementById("ff-bundle-labels");
-
- //get type
- this._types = window.arguments[0];
-
- //get defaults
- this._defaults = new Object();
- var pbs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
- var branch = pbs.getDefaultBranch("forecastfox.");
- for (var i=0; i< this._types.length; i++)
- this._defaults[this._types[i]] = this._bundle.getString(eval("'forecastfox." + this._types[i] + "'"));
-
- //set title
- var last = this._types[0].lastIndexOf(".");
- var title = this._bundle.getString(eval("'ff.labels." + this._types[0].substring(0,last) + "'"));
- document.getElementById("ff-labels-header").title = title;
-
- //set display
- this._rows = new Array();
- if (this._types.length > 1) {
- this._rows.push(document.getElementById("ff-box-title"));
- this._rows.push(document.getElementById("ff-box-label"));
- } else {
- this._rows.push(document.getElementById("ff-box-label"));
- }
-
- for (i=0; i< this._rows.length; i++) {
- this._rows[i].removeAttribute("hidden");
- var textbox = this._rows[i].getElementsByTagName("textbox")[0];
- var button = this._rows[i].getElementsByTagName("button")[0];
- textbox.value = this._getUTFOption(this._types[i]);
-
- if (i == this._rows.length -1) {
- textbox.focus();
- textbox.select();
- }
-
- branch = getBranch(false, null);
- if (branch.prefHasUserValue(this._types[i]))
- button.removeAttribute("disabled");
- }
-
- //setup tree and buttons
- this._populateTree();
- this.updateButtons();
-
- // disable the apply button
- document.getElementById("apply").setAttribute("disabled", true);
- },
-
- stop: function LabelsModule_stop()
- {
- this._manager = null;
- this._clipboard = null;
- this._bundle = null;
- this._types = null;
- this._defaults = null;
- this._rows = null;
- this._sortId = null;
- this._sortDirection = null;
- this._naturalOrder = null;
- this._columns = null;
- },
-
- restoreDefault: function LabelsModule_restoreDefault(aType)
- {
- for (var i=0; i<this._types.length; i++) {
- if (this._rows[i].hasAttribute("hidden"))
- continue;
- if (this._types[i].indexOf(aType) != -1) {
- var textbox = this._rows[i].getElementsByTagName("textbox")[0];
- textbox.value = this._defaults[this._types[i]];
- }
- }
-
- this.updateButtons();
- },
-
- copy: function LabelsModule_copy(aType)
- {
- var tree = document.getElementById("ff-tree-vars");
- if (tree.currentIndex == -1)
- return;
-
- var item = tree.contentView.getItemAtIndex(tree.currentIndex);
- var cells = item.getElementsByTagName("treecell");
- var copy_string;
-
- switch (aType) {
- case "variable" :
- copy_string = cells[0].getAttribute("label");
- break;
- case "description" :
- copy_string = cells[1].getAttribute("label");
- break;
- }
-
- this._clipboard.copyString(copy_string);
- },
-
- updateButtons: function LabelsModule_updateButtons(aEvent)
- {
- if (aEvent) {
- // don't enable the apply button for button presses
- if (aEvent.explicitOriginalTarget.localName != "textbox")
- return;
- }
-
- //enable apply
- document.getElementById("apply").removeAttribute("disabled");
-
- //set restore buttons
- for (var i=0; i<this._types.length; i++) {
- var textbox = this._rows[i].getElementsByTagName("textbox")[0];
- var button = this._rows[i].getElementsByTagName("button")[0];
- if (textbox.value != this._defaults[this._types[i]])
- button.removeAttribute("disabled");
- else
- button.setAttribute("disabled", "true");
- }
- },
-
- accept: function LabelsModule_accept(aClose)
- {
- //set values
- var branch = getBranch(false, null);
- for (var i=0; i<this._types.length; i++) {
- var textbox = this._rows[i].getElementsByTagName("textbox")[0];
- if (textbox.value == this._defaults[this._types[i]] &&
- branch.prefHasUserValue(this._types[i]))
- branch.clearUserPref(this._types[i]);
- else
- this._setUTFOption(this._types[i], textbox.value);
- }
-
- //close
- if (aClose)
- window.close();
- else
- document.getElementById("apply").setAttribute("disabled", "true");
- },
-
- setSort: function LabelsModule_setSort(aType)
- {
- var sortId = "ff-col-"+aType;
- var column = document.getElementById(sortId);
- var direction = column.getAttribute("sortDirection");
-
- // set the sort according to the current sort direction on the column
- if (direction == "ascending")
- this._setSort(aType, "descending");
- else if (direction == "descending")
- this._setSort(aType, "natural");
- else
- this._setSort(aType, "ascending");
- },
-
- _setSort: function LabelsModule__setSort(aType, aDirection)
- {
- // set the internal sort id and direction
- this._sortId = "ff-col-"+aType;
- this._sortDirection = aDirection;
-
- var columns = document.getElementById("ff-tree-columns");
- columns = columns.getElementsByTagName("treecol");
-
- // update the sort attributes
- for (var x = 0; x < columns.length; x++) {
- if (columns[x].getAttribute("id") == this._sortId)
- columns[x].setAttribute("sortDirection", aDirection);
- else
- columns[x].removeAttribute("sortDirection");
- }
-
- // sort the columns
- this._sort();
- },
-
- _sort: function LabelsModule__sort()
- {
- // return if there is no sorting
- if (!this._sortId || !this._sortDirection) return;
-
- var results = document.getElementById("ff-tree-items");
- var rows = new Array();
-
- // get the list of rows
- while (results.hasChildNodes()) {
- var result = results.removeChild(results.firstChild);
- if (result.nodeName == "treeitem")
- rows.push(result);
- }
-
- // perform the sort based on the direction
- if (this._sortDirection == "ascending")
- rows.sort(sortAscending);
- else if (this._sortDirection == "descending")
- rows.sort(sortDescending);
- else
- rows = this._naturalOrder;
-
- // then re-insert the rows
- for (var x = 0; x < rows.length; x++)
- results.appendChild(rows[x]);
- },
-
- _getUTFOption: function LabelsModule__getUTFOption(aOption)
- {
- return getPref(aOption);
- },
-
- _setUTFOption: function LabelsModule__setUTFOption(aOption, aValue)
- {
- setPref(aOption, aValue);
- },
-
- _populateTree: function LabelsModule__populateTree()
- {
- var tree = document.getElementById("ff-tree-items");
- var tooltip, type;
-
- //clear old tree data
- while (tree.hasChildNodes())
- tree.removeChild(tree.lastChild);
-
- this._naturalOrder = new Array();
-
- //populate current values
- type = this._types[0].substring(0, this._types[0].indexOf("."));
- tooltip = (this._types[0].indexOf("tooltip") != -1);
- switch (type) {
- case "dayt":
- this._setTreeData(tree, "days", 0, tooltip);
- break;
- case "dayf":
- this._setTreeData(tree, "days", 1, tooltip);
- break;
- case "swa":
- this._setTreeData(tree, "swa", 0, tooltip);
- break;
- case "radar":
- this._setTreeData(tree, "radar", 0, tooltip);
- break;
- case "cc":
- default:
- this._setTreeData(tree, "current", 0, tooltip);
- break;
- }
- },
-
- _setTreeData: function LabelsModule__setTreeData(aTree, aName, aIndex, aTooltip) {
- var data;
- var items = this._manager.parser.getItems(aName, aIndex, {});
-
- for (var j=0; j<items.length; j++) {
- var item = items[j];
- if (!aTooltip && (item.name == "nl"))
- continue;
- data = new Array(3);
- data[0] = "[" + item.name + "]";
- data[1] = item.getProperty("description");
- data[2] = this._manager.parser.getValue(aName, aIndex, item.name, null);
- var treeitem = this._addTreeRow(aTree, data, true);
-
- //get the conversions
- if (item.hasProperty("conversion")) {
- var converters = this._manager.converters.getItems(
- item.getProperty("conversion"), {});
- if (converters.length == 0)
- continue;
- var child = document.createElement("treechildren");
- for (var k=0; k<converters.length; k++) {
- var converter = converters[k];
- data = new Array(3);
- data[0] = "[" + item.name + "+" + converter.name + "]";
- data[1] = item.getProperty("description") + " - " +
- converter.getProperty("description");
- data[2] = this._manager.parser.getValue(aName, aIndex, item.name,
- converter.name);
- this._addTreeRow(child, data, false);
- }
- treeitem.setAttribute("container", "true");
- treeitem.appendChild(child);
- }
- }
- },
-
- _addTreeRow: function LabelsModule__addTreeRow(aParent, aData, aStore)
- {
- var item = document.createElement("treeitem");
- var node = document.createElement("treerow");
- var cell;
-
- for (var i=0; i<aData.length; i++) {
-
- cell = document.createElement("treecell");
- cell.setAttribute("label", aData[i]);
- cell.setAttribute("ref", this._columns[i]);
-
- // And add it onto the row.
- node.appendChild(cell);
- }
-
- // When done add the node onto the item, then the item onto the aParent.
- item.appendChild(node);
- aParent.appendChild(item);
- if (aStore)
- this._naturalOrder.push(item);
- return item;
- }
- };
-
- function sortDescending(aItem1, aItem2) {
- var label1 = getCellForId(aItem1).getAttribute("label");
- var label2 = getCellForId(aItem2).getAttribute("label");
- if (label1 > label2)
- return -1;
- else if (label1 == label2)
- return 0;
- else
- return 1;
- }
-
- function sortAscending(aItem1, aItem2) {
- return sortDescending(aItem1, aItem2) * -1;
- }
-
- function getCellForId(aTreeitem) {
- var cells = aTreeitem.getElementsByTagName("treecell");
- for (var x = 0; x < cells.length; x++) {
- if (cells[x].getAttribute("ref") == gLabels._sortId)
- return cells[x];
- }
- return null;
- }